package com.iflytek.uoamp.agent.logger.config;

import com.iflytek.uoamp.agent.common.constants.CommonConstants;
import org.apache.log4j.Logger;

import com.iflytek.uoamp.agent.core.conf.AgentCoreConfig;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * @ClassName: AgentCoreConfig
 * @Description: core模块配置信息类
 * @author qinanyuan
 * @date 2017年3月22日 下午9:08:47
 */
public class AgentLoggerConfig {
    private static AgentLoggerConfig instance = new AgentLoggerConfig();
    private AgentLoggerConfig() {
    }
    public static AgentLoggerConfig getInstance() {
        return instance;
    }

    private String host;
    private int port;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void init() throws IOException {
        // 加载默认的agent配置，端口，版本等；
        Properties prop = new Properties();
        BufferedReader in = null;
        // 加载自动发现的配置；
        try {
        	boolean isLinux = AgentCoreConfig.getInstance().isLinux();
        	if(isLinux){
        		in = new BufferedReader(new InputStreamReader(new FileInputStream(CommonConstants.LINUX_LOGGER_PATH), "UTF-8"));
        		Logger.getRootLogger().info("use linux conf uoampLogger.properties!");
        	}else{
        		in = new BufferedReader(new InputStreamReader(new FileInputStream(CommonConstants.WIN_LOGGER_PATH), "UTF-8"));
        		Logger.getRootLogger().info("use windows conf uoampLogger.properties!");
        	}
            prop.load(in);
            port = Integer.parseInt(prop.getProperty("logger_server_port"));
        } catch (Exception e) {
            Logger.getRootLogger().error(e.getMessage(), e);
        } finally {
            if (in != null) {
                in.close();
            }
            prop.clear();
        }
    }


}
